package lg.minecraft.plugin.entityclear.config;

import lg.minecraft.plugin.entityclear.LGEntityClear;
import lg.minecraft.plugin.entityclear.component.ComponentType;
import lg.minecraft.plugin.entityclear.component.EntityFilter;
import lg.minecraft.plugin.entityclear.component.EntityRemover;
import lg.minecraft.plugin.entityclear.component.WorldFilter;
import lg.minecraft.plugin.entityclear.manager.EntityTaskManager;
import lg.minecraft.plugin.entityclear.manager.ParserManager;
import lg.minecraft.plugin.entityclear.parser.EntityFilterParser;
import lg.minecraft.plugin.entityclear.parser.EntityRemoverParser;
import lg.minecraft.plugin.entityclear.parser.ParserContext;
import lg.minecraft.plugin.entityclear.parser.WorldFilterParser;
import lg.minecraft.plugin.entityclear.template.EntityClearTaskTemplate;
import lg.minecraft.plugin.library.LGPlugin;
import lg.minecraft.plugin.library.config.LGBaseConfig;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
public class ClearTaskConfig extends LGBaseConfig {

    public static final ClearTaskConfig INSTANCE = new ClearTaskConfig(LGEntityClear.getInstance(), "clear_task.yml");

    public ClearTaskConfig(LGPlugin plugin, String fileName) {
        super(plugin, fileName);
        saveDefault();
    }

    @Override
    public void load() {
        super.load();
        EntityTaskManager.INSTANCE.clear();
        for (String key : getKeys(false)) {
            if (!getBoolean(key + ".enable")) continue;
            plugin.info("正在加载清理任务: {0}", key);
            int interval = getInt(key + ".interval");
            if (interval <= 0) {
                plugin.warning("无效的清理间隔: {0}", interval);
                continue;
            }

            WorldFilter worldFilter = getWorldFilter(key);
            if (worldFilter == null) {
                plugin.warning("无效的世界过滤器: {0}", getString(key + ".world_filter.name"));
            }
            EntityFilter entityFilter = getEntityFilter(key);
            if (entityFilter == null) {
                plugin.warning("无效的实体过滤器: {0}", getString(key + ".entity_filter.name"));
                continue;
            }
            EntityRemover entityRemover = getEntityRemover(key);
            if (entityRemover == null) {
                plugin.warning("无效的实体删除器: {0}", getString(key + ".remover.name"));
                continue;
            }

            EntityClearTaskTemplate.Builder builder = new EntityClearTaskTemplate.Builder()
                    .clearInterval(interval * 1000)
                    .worldFilter(worldFilter)
                    .entityFilter(entityFilter)
                    .entityRemover(entityRemover);

            Map<Integer, String> messages = new HashMap<>();
            ConfigurationSection section = getConfigurationSection(key + ".message");
            if (section != null) {
                for (String messageTime : section.getKeys(false)) {
                    if (!messageTime.matches("^\\d+$")) continue;
                    int time = -1;
                    try {
                        time = Integer.parseInt(messageTime);
                    } catch (Exception ignored) {}
                    if (time <= -1) {
                        plugin.warning("无效的提示时间: {0}", messageTime);
                        continue;
                    }
                    String message = section.getString(messageTime);
                    if (message == null) {
                        plugin.warning("无效的提示信息: {0}", messageTime);
                        continue;
                    }
                    messages.put(time, message);
                }
                builder.messages(messages);
                String clearMessage = section.getString("clear");
                if (clearMessage != null) {
                    builder.clearMessage(clearMessage);
                }
            }

            EntityTaskManager.INSTANCE.addTask(key, builder.build().createTask());
        }
    }

    @Nullable
    public WorldFilter getWorldFilter(@NotNull String key) {
        String name = getString(key + ".world_filter.name");
        if (name == null) {
            plugin.warning("无效的世界过滤器");
            return null;
        }
        WorldFilterParser worldParser = ParserManager.INSTANCE.getWorldFilterParser(name);
        if (worldParser == null) {
            plugin.warning("无法识别的世界过滤器: {0}", getString(key + ".world.filter"));
            return null;
        }
        ParserContext parserContext = new ParserContext(plugin, getConfigurationSection(key + ".world_filter.attribute"), name, ComponentType.WORLD_FILTER);
        return worldParser.parse(parserContext);
    }

    @Nullable
    public EntityFilter getEntityFilter(@NotNull String key) {
        String name = getString(key + ".entity_filter.name");
        if (name == null) {
            plugin.warning("无效的实体过滤器");
            return null;
        }
        EntityFilterParser entityParser = ParserManager.INSTANCE.getEntityFilterParser(name);
        if (entityParser == null) {
            plugin.warning("无法识别的实体过滤器: {0}", getString(key + ".entity.filter"));
            return null;
        }
        ParserContext parserContext = new ParserContext(plugin, getConfigurationSection(key + ".entity_filter.attribute"), name, ComponentType.ENTITY_FILTER);
        return entityParser.parse(parserContext);
    }

    @Nullable
    public EntityRemover getEntityRemover(@NotNull String key) {
        String name = getString(key + ".remover.name");
        if (name == null) {
            plugin.warning("无效的实体删除器");
            return null;
        }
        EntityRemoverParser removerParser = ParserManager.INSTANCE.getEntityRemoverParser(name);
        if (removerParser == null) {
            plugin.warning("无法识别的实体删除器: {0}", getString(key + ".remover"));
            return null;
        }
        ParserContext parserContext = new ParserContext(plugin, getConfigurationSection(key + ".remover.attribute"), name, ComponentType.ENTITY_REMOVER);
        return removerParser.parse(parserContext);
    }

}
